//
//  EFMyOrderViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/10.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyOrderViewCell.h"
#import "EFMyOrderViewItem.h"
#import "EFOrderLogisticsInformationViewController.h"
#import "EFOrderDetailViewController.h"
#import "EFOrderProductItem.h"
#import "EFReturnListViewController.h"
#import "EFRequestReturnViewController.h"

@interface EFMyOrderViewCell()

@property(strong,nonatomic) UIView* headerView;
@property(strong,nonatomic) UILabel* dateLabel;
@property(strong,nonatomic) UILabel* topStatusLabel;
@property(strong,nonatomic) UIView* topLine;
@property(strong,nonatomic) UIImageView* firstGoodsIV;
@property(strong,nonatomic) UIImageView* secondGoodsIV;
@property(strong,nonatomic) UIImageView* thirdGoodsIV;
@property(strong,nonatomic) UILabel* totalCountLabel;
@property(strong,nonatomic) UIImageView* moreIV;
@property(strong,nonatomic) UIView* bottomLine;
@property(strong,nonatomic) UILabel* totalPriceLabel;
@property(strong,nonatomic) UILabel* bottomStatusLabel;
@property(strong,nonatomic) UIButton* operatorBtn;


@property(strong,nonatomic) EFMyOrderViewItem* item;

@end

@implementation EFMyOrderViewCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)setObject:(EFMyOrderViewItem *)item{
    self.item = item;
    [self.dateLabel setText:item.created_at];
    [self.dateLabel sizeToFit];
    
    [self.topStatusLabel setText:item.status_name];
    [self.topStatusLabel sizeToFit];
    self.topStatusLabel.left = kScreenWidth - self.topStatusLabel.width - 20;
    
    
    NSString* totalCount = NSStringF(@"共计%d件",item.total);
    [self.totalCountLabel setText:totalCount];
    [self.totalCountLabel sizeToFit];
    
    if(item.total == 1){
        self.secondGoodsIV.hidden = YES;
        self.thirdGoodsIV.hidden = YES;
    }else if(item.total == 2){
        self.thirdGoodsIV.hidden = YES;
    }
    
    for(int i = 0; i < item.info.count ; ++i){
        EFOrderProductItem *orderItem = [item.info objectAtIndex:i];
        if(i == 0){
            [self.firstGoodsIV setImageWithURL:[NSURL URLWithString:orderItem.img]];
        }else if(i == 1){
            [self.secondGoodsIV setImageWithURL:[NSURL URLWithString:orderItem.img]];
        }else if(i == 2){
            [self.thirdGoodsIV setImageWithURL:[NSURL URLWithString:orderItem.img]];
        }
    }
    
    
    NSMutableAttributedString * strPrice = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"总价 ￥%@",item.amount]];
    
    NSRange range = NSMakeRange([[strPrice string] rangeOfString:@"￥"].location, [strPrice length] - 3 );
    [strPrice addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(0x69BA45) range:range];
    
    [self.totalPriceLabel setAttributedText:strPrice];
    [self.totalPriceLabel sizeToFit];
    
//    [self.bottomStatusLabel setText:item.bottomStatus];
//    [self.bottomStatusLabel sizeToFit];
    
    if(item.is_show_btn_returnrequest == 1 || item.is_show_btn_cancelorder == 1 || item.is_show_btn_deleteorder == 1 || item.is_show_btn_paynow == 1 || item.is_show_btn_buyagain == 1 || item.is_show_btn_returndetail == 1 ){
        self.operatorBtn.hidden = NO;
        if(item.is_show_btn_paynow  ==1 ){
            [self.operatorBtn setTitle:@"立即支付" forState:UIControlStateNormal];
        }else if(item.is_show_btn_buyagain == 1){
            [self.operatorBtn setTitle:@"再次购买" forState:UIControlStateNormal];
        }else if(item.is_show_btn_returnrequest == 1){
            [self.operatorBtn setTitle:@"申请退换货" forState:UIControlStateNormal];
        }else if(item.is_show_btn_cancelorder == 1){
            [self.operatorBtn setTitle:@"取消订单" forState:UIControlStateNormal];
        }else if(item.is_show_btn_deleteorder == 1){
            [self.operatorBtn setTitle:@"删除订单" forState:UIControlStateNormal];
        }else if(item.is_show_btn_returndetail == 1){
            [self.operatorBtn setTitle:@"退换货详情" forState:UIControlStateNormal];
        }
    }else{
        self.operatorBtn.hidden = YES;
    }
    
    if(item.isForTransHomeDetail){
        self.headerView.hidden = true;
        self.dateLabel.hidden = true;
        self.topStatusLabel.hidden = true;
        self.totalPriceLabel.hidden = true;
        self.topStatusLabel.hidden = true;
        self.operatorBtn.hidden = true;
         
        self.topLine.top =   7;
        self.topLine.width =   kScreenWidth - 40;;
        self.topLine.left =   20;
        
        self.bottomLine.width =   kScreenWidth - 40;;
        self.bottomLine.left =   20;
    }else{
        self.dateLabel.left = 30;
        self.dateLabel.top = self.headerView.bottom +  15;
        
        self.topStatusLabel.top = self.headerView.bottom + 13;
        self.topStatusLabel.left = kScreenWidth - 15 - self.topStatusLabel.width;
        self.topLine.top = self.dateLabel.bottom + 7;
    }
}

-(void)initView{
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.dateLabel];
    [self.contentView addSubview:self.topStatusLabel];
    
    [self.contentView addSubview:self.topLine];
    [self.contentView addSubview:self.firstGoodsIV];
    [self.contentView addSubview:self.secondGoodsIV];
    [self.contentView addSubview:self.thirdGoodsIV];
    [self.contentView addSubview:self.totalCountLabel];
    [self.contentView addSubview:self.moreIV];
    [self.contentView addSubview:self.bottomLine];
    
    [self.contentView addSubview:self.totalPriceLabel];
    [self.contentView addSubview:self.bottomStatusLabel];
    [self.contentView addSubview:self.operatorBtn];
    
}

-(void)layoutSubviews{
    self.firstGoodsIV.left = 20;
    self.firstGoodsIV.top = self.topLine.bottom + 9;
    
    self.secondGoodsIV.left = self.firstGoodsIV.right + 7;
    self.secondGoodsIV.top = self.topLine.bottom + 9;
    
    self.thirdGoodsIV.left = self.secondGoodsIV.right + 7;
    self.thirdGoodsIV.top = self.topLine.bottom + 9;
    
    self.moreIV.top = self.topLine.bottom + 39;
    self.moreIV.left = kScreenWidth - self.moreIV.width - 20;
    
    self.totalCountLabel.left = self.moreIV.left - 1 - self.totalCountLabel.width;
    self.totalCountLabel.top = self.topLine.bottom + 42;
    
    self.bottomLine.top = self.firstGoodsIV.bottom + 9;
    
    self.totalPriceLabel.left  = 20;
    self.totalPriceLabel.top = self.bottomLine.bottom + 13;
    
    self.bottomStatusLabel.left = self.totalPriceLabel.right + 15;
    self.bottomStatusLabel.top  = self.bottomLine.bottom + 13;

    self.operatorBtn.top = self.bottomLine.bottom + 9;
    self.operatorBtn.left = kScreenWidth - self.operatorBtn.width -  20;
    
}

-(void)onClickOperator{
    if(self.item.is_show_btn_paynow){ //立即支付
        EFOrderDetailViewController *orderDetail = [[EFOrderDetailViewController alloc] initWithOrderNumber:self.item.order_no];
        [ICNavigator pushViewController:orderDetail animated:YES];
    }else if(self.item.is_show_btn_buyagain == 1){ //再次购买
       // [self.operatorBtn setTitle:@"再次购买" forState:UIControlStateNormal];
    }else if(self.item.is_show_btn_returnrequest == 1){
        EFRequestReturnViewController* request = [[EFRequestReturnViewController alloc]initWithOrderNo:self.item.order_no];
        [ICNavigator pushViewController:request animated:YES];
    }else if(self.item.is_show_btn_cancelorder == 1){ //取消订单
        if(self.operator != NULL){
            self.operator(1,self.item.id);
        }
    }else if(self.item.is_show_btn_deleteorder == 1){ //删除订单
        if(self.operator != NULL){
            self.operator(0,self.item.id);
        }
    }else if(self.item.is_show_btn_returndetail == 1){
        EFReturnListViewController* returnList = [[EFReturnListViewController alloc] initWithOrderId:self.item.id];
        [ICNavigator pushViewController:returnList animated:YES];
    }
}

-(UIView*)headerView{
    if(_headerView == NULL){
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth  , 10)];
        _headerView.backgroundColor = RGBCOLOR(0xF4F4F4);
    }
    return _headerView;
}


-(UILabel *)dateLabel{
    if(_dateLabel == NULL){
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 11)];
        _dateLabel.font = [UIFont systemFontOfSize:13];
        _dateLabel.textColor = kBlack999;
    }
    return _dateLabel;
}


-(UILabel *)topStatusLabel{
    if(_topStatusLabel == NULL){
        _topStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 62, 11)];
        _topStatusLabel.font = [UIFont systemFontOfSize:13];
        _topStatusLabel.textColor = RGBCOLOR(0x8D95A0);
    }
    return _topStatusLabel;
}

-(UIView *)topLine{
    if(_topLine == NULL){
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        _topLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _topLine;
}

-(UIImageView *)firstGoodsIV{
    if(_firstGoodsIV == NULL){
        _firstGoodsIV = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _firstGoodsIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _firstGoodsIV.contentMode = UIViewContentModeScaleAspectFill;
        _firstGoodsIV.clipsToBounds = YES;
    }
    return _firstGoodsIV;
}

-(UIImageView *)secondGoodsIV{
    if(_secondGoodsIV == NULL){
        _secondGoodsIV = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _secondGoodsIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _secondGoodsIV.contentMode = UIViewContentModeScaleAspectFill;
        _secondGoodsIV.clipsToBounds = YES;
    }
    return _secondGoodsIV;
}

-(UIImageView *)thirdGoodsIV{
    if(_thirdGoodsIV == NULL){
        _thirdGoodsIV = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 80, 80)];
        _thirdGoodsIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _thirdGoodsIV.contentMode = UIViewContentModeScaleAspectFill;
        _thirdGoodsIV.clipsToBounds = YES;
    }
    return _thirdGoodsIV;
}


-(UILabel *)totalCountLabel{
    if(_totalCountLabel == NULL){
        _totalCountLabel = [[UILabel alloc ] initWithFrame:CGRectMake(0, 0, 50, 15)];
        _totalCountLabel.font = [UIFont systemFontOfSize:13];
        _totalCountLabel.textColor = kBlack999;
    }
    return _totalCountLabel;
}


-(UIImageView *)moreIV{
    if(_moreIV == NULL){
        _moreIV = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 20, 20)];
        [_moreIV setImage:IMG(@"more_icon_me_default")];
    }
    return _moreIV;
}

-(UIView *)bottomLine{
    if(_bottomLine == NULL){
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        _bottomLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _bottomLine;
}

-(UILabel *)totalPriceLabel{
    if(_totalPriceLabel == NULL){
        _totalPriceLabel = [[UILabel alloc ] initWithFrame:CGRectMake(0, 0, 50, 15)];
        _totalPriceLabel.font = [UIFont systemFontOfSize:13];
        _totalPriceLabel.textColor = kBlack333;
    }
    return _totalPriceLabel;
}

-(UILabel *)bottomStatusLabel{
    if(_bottomStatusLabel == NULL){
        _bottomStatusLabel = [[UILabel alloc ] initWithFrame:CGRectMake(0, 0, 91, 16)];
        _bottomStatusLabel.font = [UIFont systemFontOfSize:13];
        _bottomStatusLabel.textColor = kBlack999;
    }
    return _bottomStatusLabel;
}

-(UIButton *)operatorBtn{
    if(_operatorBtn == NULL){
        _operatorBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 76, 25)];
        [_operatorBtn setBackgroundImage:IMG(@"linebutton_bg_default") forState:UIControlStateNormal];
        [_operatorBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        _operatorBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _operatorBtn.titleLabel.textColor = RGBCOLOR(0x69BA45);
          [_operatorBtn addTarget:self action:@selector(onClickOperator) forControlEvents:UIControlEventTouchUpInside];
    }
    return _operatorBtn;
}




@end
